package com.gxa.service.impl;

import com.gxa.common.PaginationResult;
import com.gxa.mapper.EncodSetMapper;
import com.gxa.pojo.Codrule;
import com.gxa.service.EncodSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zxy
 * Date: 2020/10/31 10:47
 * Description:
 * Version: V1.0
 */
@Service
public class EncodSetServiceImpl implements EncodSetService {

    @Autowired(required = false)
    private EncodSetMapper encodSetMapper;

    @Override
    public int addEncodSet(Codrule codrule) {
        String example = null;
        if(codrule.getDates() == 1){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String s = df.format(new Date()).toString();
            String s1 = s.replace("-","");
            example = codrule.getCodrulePre()+s1;
        }
        if(codrule.getTimes() == 1){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = df.format(new Date()).toString();
            String s1 = s.substring(11,s.length());
            String s2 = s1.replace(":","");
            example = example + s2;
        }
        example = example + codrule.getSerialSta();
        codrule.setCodruleExample(example);
        return encodSetMapper.addEncodSet(codrule);
    }
    @Override
    public PaginationResult queryEncodSet(int currentPage, int pageSize) {
        PaginationResult paginationResult = new PaginationResult();
        //当前页码数
        paginationResult.setCurrentPage(currentPage);
        //每页显示条数
        paginationResult.setPageSize(pageSize);
        //总条数
        int totalSize = getEncodSet();
        //总页数
        int totalPage = totalSize / pageSize;
        if(totalSize % pageSize !=0){
            totalPage +=1;
        }
        paginationResult.setTotalPage(totalPage);
        paginationResult.setTotalSize(totalSize);
        int param1 = (currentPage-1)*pageSize;
        int param2 = pageSize;
        List<Codrule> codruleList = encodSetMapper.queryCodrule(param1,param2);
        paginationResult.setData(codruleList);
        return paginationResult;
    }
    @Override
    public int updateEncodSet(Codrule codrule) {
        if(codrule.getDates() == 0 && codrule.getTimes() == 1){
            String s1 = codrule.getCodruleExample().substring(0,codrule.getCodrulePre().length());
            String s2 = codrule.getCodruleExample().substring(codrule.getCodrulePre().length()+9,codrule.getCodruleExample().length());
            String example = s1+s2;
            codrule.setCodruleExample(example);
        }
        if(codrule.getDates() == 1 && codrule.getTimes() == 0){
            String s1 = codrule.getCodruleExample().substring(0,codrule.getCodrulePre().length()+8);
            String s2 = codrule.getCodruleExample().substring(codrule.getCodrulePre().length()+15,codrule.getCodruleExample().length());
            String example = s1+s2;
            codrule.setCodruleExample(example);
        }
        if(codrule.getDates() == 0 && codrule.getTimes() == 0) {
            String s1 = codrule.getCodruleExample().substring(0, codrule.getCodrulePre().length());
            String s2 = codrule.getCodruleExample().substring(codrule.getCodrulePre().length() + 15, codrule.getCodruleExample().length());
            String example = s1 + s2;
            codrule.setCodruleExample(example);
        }
        return encodSetMapper.updateEncodSet(codrule);
    }
    @Override
    public int delEncodSet(int codruleId) {
        return encodSetMapper.delEncodSet(codruleId);
    }

    public int getEncodSet() {
        return encodSetMapper.getEncodSet();
    }
}
